using System;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;
using Jbe.TestSuite.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI;
using Jbe.TestSuite.Infrastructure.Interface.Services;

namespace Jbe.TestSuite.RTFEditor
{
    public partial class EditorView : UserControl, IEditorView
    {
        public event EventHandler ModifiedChanged;

        private IEditManager editManager;

        [ServiceDependency]
        public IEditManager EditManager
        {
            set 
            { 
                editManager = value;
                editManager.Register(richTextBox);
            }
        }

        public bool Modified
        {
            get { return richTextBox.Modified; }
            set { richTextBox.Modified = value; }
        }

        public EditorView()
        {
            InitializeComponent();

            richTextBox.ModifiedChanged += new EventHandler(delegate(object sender, EventArgs e) 
            {
                OnModifiedChanged();
            });
        }

        public void Open(string fileName)
        {
            richTextBox.LoadFile(fileName);
        }

        public void Save(string fileName)
        {
            richTextBox.SaveFile(fileName);
        }

        protected override void OnLoad(EventArgs e)
        {
            presenter.OnViewReady();
            base.OnLoad(e);
        }

        protected virtual void OnModifiedChanged()
        {
            if (ModifiedChanged != null) { ModifiedChanged(this, new EventArgs()); }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                editManager.Deregister(richTextBox);

                if (presenter != null) { presenter.Dispose(); }
                if (components != null) { components.Dispose(); }
            }

            base.Dispose(disposing);
        }
    }
}

